iOS 自定义-苹方字体的使用

不是系统已经自带苹方字体了为什么还要自定义呢?因为苹方字体是在iOS 9引入的,所以如果要支持iOS 9以下都要手动引入苹方字体。

添加苹方字体到项目中

  • 获取苹方字体,Command+G输入/System/Library/Fonts/PingFang.ttc

  • 直接拖到将PingFang.ttc文件拖到项目中

  • 在 info.plist文件中添加Fonts provided by application,并设置字体包名称
    添加设置字体

我在iOS 8系统的模拟器中没有在Info.plist中添加Fonts provided by application,也已经可以正常使用苹方字体了

获取当前系统支持的字体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
for (NSString *fontFamilyName in UIFont.familyNames) {
NSLog(@"familyName:%@", fontFamilyName);
for (NSString *fontName in [UIFont fontNamesForFamilyName:fontFamilyName]) {
NSLog(@"fontName:%@", fontName);
}
}
打印结果省略了苹方字体以外的字体
/**
……
2018-04-28 11:07:52.217401+0800 QYLY[5900:238352] familyName:PingFang TC
2018-04-28 11:07:52.218150+0800 QYLY[5900:238352] fontName:PingFangTC-Regular
2018-04-28 11:07:52.218609+0800 QYLY[5900:238352] fontName:PingFangTC-Thin
2018-04-28 11:07:52.219340+0800 QYLY[5900:238352] fontName:PingFangTC-Medium
2018-04-28 11:07:52.220841+0800 QYLY[5900:238352] fontName:PingFangTC-Semibold
2018-04-28 11:07:52.222403+0800 QYLY[5900:238352] fontName:PingFangTC-Light
2018-04-28 11:07:52.223328+0800 QYLY[5900:238352] fontName:PingFangTC-Ultralight
……
2018-04-28 11:07:52.299506+0800 QYLY[5900:238352] familyName:PingFang HK
2018-04-28 11:07:52.300103+0800 QYLY[5900:238352] fontName:PingFangHK-Medium
2018-04-28 11:07:52.300944+0800 QYLY[5900:238352] fontName:PingFangHK-Thin
2018-04-28 11:07:52.301597+0800 QYLY[5900:238352] fontName:PingFangHK-Regular
2018-04-28 11:07:52.302162+0800 QYLY[5900:238352] fontName:PingFangHK-Ultralight
2018-04-28 11:07:52.302652+0800 QYLY[5900:238352] fontName:PingFangHK-Semibold
2018-04-28 11:07:52.303029+0800 QYLY[5900:238352] fontName:PingFangHK-Light
……
2018-04-28 11:07:52.423305+0800 QYLY[5900:238352] familyName:PingFang SC
2018-04-28 11:07:52.423714+0800 QYLY[5900:238352] fontName:PingFangSC-Medium
2018-04-28 11:07:52.424151+0800 QYLY[5900:238352] fontName:PingFangSC-Semibold
2018-04-28 11:07:52.424397+0800 QYLY[5900:238352] fontName:PingFangSC-Light
2018-04-28 11:07:52.424621+0800 QYLY[5900:238352] fontName:PingFangSC-Ultralight
2018-04-28 11:07:52.424934+0800 QYLY[5900:238352] fontName:PingFangSC-Regular
2018-04-28 11:07:52.425314+0800 QYLY[5900:238352] fontName:PingFangSC-Thin
……
*/

苹方字体怎么会有PingFang SC、PingFang TC、PingFang HK三种呢?对应的是苹方-简、苹方-繁、苹方-港,而且都有六种字重,具体可以在字体册中查看。

苹方-简 中黑体 PingFangSC-Medium
苹方-简 中粗体 PingFangSC-Semibold
苹方-简 细体 PingFangSC-Light
苹方-简 极细体 PingFangSC-Ultralight
苹方-简 常规体 PingFangSC-Regular
苹方-简 纤细体 PingFangSC-Thin
字体册-苹方

设置苹方字体

1
2
3
4
5
6
7
8
9
// 苹方字体,默认为PingFangSC-Regular
UIFont *font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
NSString *familyName = font.familyName;
NSString *fontName = font.fontName;
NSLog(@"familyName:%@\nfontName:%@", familyName, fontName);
/**
familyName:PingFang SC
fontName:PingFangSC-Regular
*/

扩展

每次都要输入字体名称比较麻烦,并且还要支持iOS 9以下,使用分类方法比较好。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, FontWeightStyle) {
FontWeightStyleMedium, // 中黑体
FontWeightStyleSemibold, // 中粗体
FontWeightStyleLight, // 细体
FontWeightStyleUltralight, // 极细体
FontWeightStyleRegular, // 常规体
FontWeightStyleThin, // 纤细体
};
@interface UIFont (Ex)
/**
苹方字体
@param fontWeight 字体粗细(字重)
@param fontSize 字体大小
@return 返回指定字重大小的苹方字体
*/
+ (UIFont *)pingFangSCWithWeight:(FontWeightStyle)fontWeight size:(CGFloat)fontSize;
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#import "UIFont+Ex.h"
@implementation UIFont (Ex)
+ (UIFont *)pingFangSCWithWeight:(FontWeightStyle)fontWeight size:(CGFloat)fontSize {
if (fontWeight < FontWeightStyleMedium || fontWeight > FontWeightStyleThin) {
fontWeight = FontWeightStyleRegular;
}
NSString *fontName = @"PingFangSC-Regular";
switch (fontWeight) {
case FontWeightStyleMedium:
fontName = @"PingFangSC-Medium";
break;
case FontWeightStyleSemibold:
fontName = @"PingFangSC-Semibold";
break;
case FontWeightStyleLight:
fontName = @"PingFangSC-Light";
break;
case FontWeightStyleUltralight:
fontName = @"PingFangSC-Ultralight";
break;
case FontWeightStyleRegular:
fontName = @"PingFangSC-Regular";
break;
case FontWeightStyleThin:
fontName = @"PingFangSC-Thin";
break;
}
UIFont *font = [UIFont fontWithName:fontName size:fontSize];
return font ?: [UIFont systemFontOfSize:fontSize];
}
@end